package marmalius.item;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class ItemService {
	private static ItemService instance = new ItemService();
	private Map<Integer, ItemTemplate> items;
	
	public ItemService() {
		items = new HashMap<Integer, ItemTemplate>();
	}
	
	public boolean loadItemTemplate() {
		try {
			int nb = 0;
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM item_template");
			while (rs.next()) {
				ItemTemplate tmp = new ItemTemplate(rs.getInt("id"));
				tmp.setName(rs.getString("name"));
				tmp.setType(rs.getInt("type"));
				tmp.setStackable(rs.getBoolean("stackable"));
				for (int i = 0; i < 12; i++)
					tmp.setBonus(i, rs.getInt("b"+i));
				items.put(rs.getInt("id"), tmp);
				
				//newItemList(rs.getInt("id"), rs.getString("name"), rs.getString("description"), rs.getDouble("shopvalue"), rs.getDouble("lowalch"), rs.getDouble("highalch"), Bonuses);
				nb++;
			}
			rs.close();
			LogService.getInstance().logInfo("item_template load ("+nb+")");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load item_template");
			LogService.getInstance().logError(ex.getMessage());
			return false;
		}
	}
	
	public ItemTemplate getItemTemplate(int id) {
		return items.get(id);
	}
	
	public static ItemService getInstance() {
		return instance;
	}
	
}
